/*
  @description: 测试老姚的小广告
 * @author: L.Wen
 * @create: 2018-09-29 14:21
 */

import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import java.util.*;

public class Test_IP {
    private static WebDriver driver;
    private WebElement webElement = null;

    static {
        //告诉系统chromedriver.exe的位置，打开Chrome浏览器
        System.setProperty("webdriver.chrome.driver", "chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    /**
     * 打开原始连接
     */
    @Test()
    public void openBD() {
        System.out.println("Test openBD! \n");
    }

    /**
     * 打开1Call标签
     * 自动执行5次
     */
    @Test(priority = 10, invocationCount = 2)
    public void searchCSDN() throws InterruptedException {

        Set<String> windows = driver.getWindowHandles();
        List<String> windowsArr = new ArrayList<>(windows);
        driver.switchTo().window(windowsArr.get(0));
        // 1.先打开西刺获取动态IP
        System.out.println("Test openXiCi! \n");
        driver.get("http://www.xicidaili.com/nt/");
        // 2.获取动态IP
        System.out.println("Test searchIP! \n");
        webElement = driver.findElement(By.xpath("//table[@id=\"ip_list\"]"));
        List<WebElement> webElements = webElement.findElements(By.xpath("//tr[@class=\"odd\"]"));
        System.out.println("size: " + webElements.size());
        List<String> ipList = new ArrayList<>();
        Map<String, String> ipMap = new HashMap<>();
        if (webElements.size() > 0) {

            for (int i = 0; i < webElements.size(); i++) {
                String ip = webElements.get(i).findElement(By.xpath(".//td[2]")).getText();
                String port = webElements.get(i).findElement(By.xpath(".//td[3]")).getText();
                ipList.add(ip);
                ipMap.put(ip, port);
            }
        }

        // 3.改变代理IP
        Random random = new Random();
        int i = random.nextInt(ipList.size());
        String ip = ipList.get(i);
        String port = ipMap.get(ip);

        String proxyIpAndPort = ip + ":" + port;
        DesiredCapabilities cap = new DesiredCapabilities();
        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
        cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
        cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
        System.setProperty("http.nonProxyHosts", "localhost");
        cap.setCapability(CapabilityType.PROXY, proxy);
        System.out.println(proxyIpAndPort);

        /*
        // 方法二
        String ip_1 = "61.128.208.94";
        String port_1 = "3128";
        System.setProperty("http.maxRedirects", "50");
        System.getProperties().setProperty("proxySet", "true");
        System.getProperties().setProperty("http.proxyHost", ip_1);
        System.getProperties().setProperty("http.proxyPort", port_1);
        // 针对https也开启代理
        System.getProperties().setProperty("https.proxyHost", ip_1);
        System.getProperties().setProperty("https.proxyPort", "" + port_1);
*/

        // 4.再打开测试页
        driver.get("http://www.ip138.com/");
        Thread.sleep(3001);
        driver.manage().deleteAllCookies();
//        driver.close();

    }

}

